//
//  LKAnimation.m
//  自定义转场动画
//
//  Created by Lukj on 2017/4/25.
//  Copyright © 2017年 lukj. All rights reserved.
//

#import "LKAnimation.h"

@implementation LKAnimation {
    //  记录转场上下文
    __weak id <UIViewControllerContextTransitioning> _transitionContext;
    //  记录是否dismiss
    BOOL _hasPersent;
}
//  设置负责展示转场动画的对象时调用
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    //  注意要重置标记, 因为trackVC不会被释放, 导致动画器也不会被释放, 标记不会自动重置
    _hasPersent = NO;
    return self;
}
//  设置负责解除转场动画的对象时调用
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    //  dismiss会走这个方法 标记改为yes
    _hasPersent = YES;
    return self;
    
}

#pragma mark - UIViewControllerAnimatedTransitioning
//  设置转场的时长
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    
    return 0.5f;
}
//  实现转场动画时调用
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    //  记录转场上下文
    _transitionContext = transitionContext;
    //  获取目标控制器视图
    UIView *destView = [transitionContext viewForKey:UITransitionContextToViewKey];
    //  获取来源控制器视图
    UIView *source = [transitionContext viewForKey:UITransitionContextFromViewKey];
    //  获取转场容器视图
    UIView *containerView = [transitionContext containerView];
    
    if (!_hasPersent) {// !_hasPersent 为YES 这是展示动画 如果解除动画不添加
        
        //  进行自定义转场时, 需要手动将目标添加到视图界面上
        [containerView addSubview:destView];
        destView.frame = containerView.bounds;
    }
    
    UIView *contextView = _hasPersent? source : destView;
    
    //  在目标视图上设置遮罩动画
    [self custemAnimInView:contextView];
    
    
    
}


- (void)custemAnimInView:(UIView *)view {
    
    //  创建形状图层
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    
    //  起始位置
    //  距离上和右的间距
    CGFloat topMargin = 33;
    CGFloat rightMargin = 21;
    //  半径
    CGFloat radius = 15;
    //  屏幕的宽/高
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    //  开始位置
    CGRect sourcrRect = CGRectMake(screenW - rightMargin - 2 * radius, topMargin, 2 * radius, 2 * radius);
    CGPathRef sourcePathRef = [UIBezierPath bezierPathWithOvalInRect:sourcrRect].CGPath;
    
    //  结束位置
    //  sqrt开方 求斜边
    CGFloat endRadius = sqrt((screenH * screenH) + (screenW * screenW));
    //  结束位置 CGRectInset(原始尺寸, X方向缩放的维度, Y方向缩放的维度);
    CGRect endRect = CGRectInset(sourcrRect, -endRadius, -endRadius);
    
    //  设置形状
    CGPathRef endPathRef = [UIBezierPath bezierPathWithOvalInRect:endRect].CGPath;
    //  设置遮罩
    view.layer.mask = shapeLayer;
    
    //  对layer设置属性动画, 必须使用CA动画, 不能使用UIView的块动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    
    if (!_hasPersent) {//   展示动画
        //  设置动画始末值
        basicAnimation.fromValue = (__bridge id _Nullable)(sourcePathRef);
        basicAnimation.toValue = (__bridge id _Nullable)(endPathRef);
    }else {//   解除动画
        //  设置动画始末值
        basicAnimation.fromValue = (__bridge id _Nullable)(endPathRef);
        basicAnimation.toValue = (__bridge id _Nullable)(sourcePathRef);
    }
    
    //  设置动画时长
    basicAnimation.duration = [self transitionDuration:_transitionContext];
    //  保留动画最终的内容
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fillMode = kCAFillModeForwards;
    //  设置动画的代理
    basicAnimation.delegate = self;
    //  添加动画, 设置改变哪个对象的属性, 就把动画添加到哪个对象上
    [shapeLayer addAnimation:basicAnimation forKey:nil];

}

#pragma mark - CAAnimationDelegate
//  动画完成后调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    [_transitionContext completeTransition:YES];
}

@end
